package com.client.ui;

import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.Paintable;
import com.vaadin.terminal.gwt.client.UIDL;

/**
 * VDesignTimeDummy
 * User: abiel
 * Date: 4/26/12
 * Time: 12:42 PM
 * Description:
 */
public class VDesignTimeDummy extends Widget implements Paintable, ClickHandler {

    /**
     * Set the CSS class name to allow styling.
     */
    public static final String CLASSNAME = "v-designtimedummy";

    public static final String CLICK_EVENT_IDENTIFIER = "click";

    /**
     * The client side widget identifier
     */
    protected String paintableId;

    /**
     * Reference to the server connection object.
     */
    protected ApplicationConnection client;

    /**
     * The constructor should first call super() to initialize the component and
     * then handle any initialization relevant to Vaadin.
     */
    public VDesignTimeDummy() {
        // TODO This example code is extending the GWT Widget class so it must set a root element.
        // Change to a proper element or remove this line if extending another widget.
        setElement(Document.get().createDivElement());

        // This method call of the Paintable interface sets the component
        // style name in DOM tree
        setStyleName(CLASSNAME);

        // Tell GWT we are interested in receiving click events
        sinkEvents(Event.ONCLICK);
        // Add a handler for the click events (this is similar to FocusWidget.addClickHandler())
        addDomHandler(this, ClickEvent.getType());
    }

    /**
     * Called whenever an update is received from the server
     */
    public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
        // This call should be made first.
        // It handles sizes, captions, tooltips, etc. automatically.
        if (client.updateComponent(this, uidl, true)) {
            // If client.updateComponent returns true there has been no changes and we
            // do not need to update anything.
            return;
        }

        // Save reference to server connection object to be able to send
        // user interaction later
        this.client = client;

        // Save the client side identifier (paintable id) for the widget
        paintableId = uidl.getId();

        // Process attributes/variables from the server
        // The attribute names are the same as we used in
        // paintContent on the server-side
        int clicks = uidl.getIntAttribute("clicks");
        String message = uidl.getStringAttribute("message");

        getElement().setInnerHTML("After <b>" + clicks + "</b> mouse clicks:\n" + message);

    }

    /**
     * Called when a native click event is fired.
     *
     * @param event the {@link com.google.gwt.event.dom.client.ClickEvent} that was fired
     */
    public void onClick(ClickEvent event) {
        // Send a variable change to the server side component so it knows the widget has been clicked
        String button = "left click";
        // The last parameter (immediate) tells that the update should be sent to the server
        // right away
        client.updateVariable(paintableId, CLICK_EVENT_IDENTIFIER, button, true);
    }
}
